package com.msh.frame.common.util;

import com.msh.frame.client.define.StringDef;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

public class DateUtil {

  private static final DateTimeFormatter formatter = DateTimeFormatter
      .ofPattern(StringDef.DEFAULT_DATE_TIME_FORMAT);
  private static final long ZONE_OFFSET = (long) TimeZone.getDefault().getRawOffset();
  private static final long ONE_DAY_MILLI_SECOUND = 24 * 60 * 60 * 1000L;

  public static String format(Date date, String formatter) {
    if (null == date) {
      return StringDef.EMPTY;
    }
    return new SimpleDateFormat(formatter).format(date);
  }

  public static String format(long time, String formatter) {
    return format(new Date(time), formatter);
  }

  public static String format(Date date) {
    if (null == date) {
      return StringDef.EMPTY;
    }
    return new SimpleDateFormat(StringDef.DEFAULT_DATE_TIME_FORMAT).format(date);
  }

  public static String format(long time) {
    return format(new Date(time));
  }

  public static String format(LocalDateTime localDateTime) {
    if (null == localDateTime) {
      return StringDef.EMPTY;
    }
    return formatter.format(localDateTime);
  }

  /**
   * 获取当天经过多少毫秒
   */
  public static long getPassDayMilliSecond() {
    long past = (System.currentTimeMillis() + ZONE_OFFSET) % (ONE_DAY_MILLI_SECOUND);
    return past;
  }

  /**
   * 获取当天剩余多少毫秒
   */
  public static long getlastDayMilliSecond() {
    long last = ONE_DAY_MILLI_SECOUND - getPassDayMilliSecond();
    return last;
  }

  public static int getDayNum(LocalDate localDate){
    return localDate.getYear() * 10000 + localDate.getMonthValue() * 100 + localDate.getDayOfMonth();
  }

  public static int getDayNum(){
    return getDayNum(LocalDate.now());
  }

  public static String formatDayNum(int dayNum, String format){
    return String.format(format, dayNum/10000, dayNum/100%100, dayNum%100);
  }

  public static String formatDayNum(int dayNum){
    return formatDayNum(dayNum, "%s-%s-%s");
  }

  public static int getTimeNum(LocalTime localTime){
    return localTime.getHour() * 10000 + localTime.getMinute() * 100 + localTime.getSecond();
  }

  public static int getTimeNum(){
    return getTimeNum(LocalTime.now());
  }

  public static String formatTimeNum(int timeNum, String format){
    return String.format(format, timeNum/10000, timeNum/100%100, timeNum%100);
  }

  public static String formatTimeNum(int timeNum){
    return formatDayNum(timeNum, "%s:%s:%s");
  }

  public static int getTimeMinuteNum(LocalTime localTime){
    return localTime.getHour() * 100 + localTime.getMinute();
  }

  public static int getTimeMinuteNum(){
    return getTimeNum(LocalTime.now());
  }

  public static String formatTimeMinuteNum(int timeNum, String format){
    return String.format(format, timeNum/100, timeNum%100);
  }

  public static String formatTimeMinuteNum(int timeNum){
    return formatDayNum(timeNum, "%s:%s");
  }
}
